package com.javabasic.algorithm.leetcode;

/**
 * @Author xiongmin
 * @Description
 * @Date 2021-04-07 23:36
 * @Version 1.0
 **/
public class MinimumDepthOfBinaryTree {

    public int minDepth(TreeNode root) {
        if (null == root) {
             return 0;
        }
        getMinDepth2(root,1);
        return result;
    }

    /**
     * 到叶子节点的null 节点去了，这个时候会影响结果判断
     * @param root
     * @return
     */
    private int getMinDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = 1, rightDepth = 1;
        if (root.left != null) {
            leftDepth += getMinDepth(root.left);
        }
        if (root.right != null) {
            rightDepth += getMinDepth(root.right);
        }
        return Math.min(leftDepth,rightDepth);
    }

    int result = Integer.MAX_VALUE;

    private void getMinDepth2(TreeNode root, int depth) {
        if (root == null) {
            return;
        }
        // 一定是到叶子节点去比较深度，而不是叶子节点的null 节点
        if (root.left == null && root.right == null) {
            result = Math.min(result,depth);
            return ;
        }
        getMinDepth2(root.left,depth+1);
        getMinDepth2(root.right,depth+1);
    }


}
